package com.zhoufeng.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import com.zhoufeng.db.ConnectionManager;

public class Recievedata extends Model<Recievedata> {
	private static final long serialVersionUID = 1L;
	public final static Recievedata me = new Recievedata();

	public String getDeviceDataInfo(String id,String ss, Record modelappmsg,String address,int port) {
		
		
		//Record modelappmsg = Db.findFirst("select *,(select aftersetcount from thing_device_info where id = thing_appmsginfo.id limit 1) serviceaftersetcount from thing_appmsginfo where id=? and appmsg=?",id,ss) ;
		boolean isupdatesetter = false;
		int appagtersettercont = 0;
		if(modelappmsg == null){
			isupdatesetter = true;
			Connection con = null;
			try {
				con = ConnectionManager.getConnection(true);
				String sqlupdateappmsginfo = "update thing_appmsginfo set appmsg = ?,aftersetcount=0 where id=? and address=? and port=?";
				String sqlinsertappmsginfo = "insert into thing_appmsginfo (appmsg,id,address,port) values (?,?,?,?)";
				
				try {
					PreparedStatement pstmtupdateappmsginfo = con.prepareStatement(sqlupdateappmsginfo);
					pstmtupdateappmsginfo.setString(1, ss);
					pstmtupdateappmsginfo.setString(2, id);
					pstmtupdateappmsginfo.setString(3, address);
					pstmtupdateappmsginfo.setInt(4, port);
					int updateappmsginfoCon = pstmtupdateappmsginfo.executeUpdate();
					pstmtupdateappmsginfo.close();
					if (updateappmsginfoCon > 0) {
					}else{
						PreparedStatement pstmtinsertappmsginfo = con.prepareStatement(sqlinsertappmsginfo);
						pstmtinsertappmsginfo.setString(1, ss);
						pstmtinsertappmsginfo.setString(2, id);
						pstmtinsertappmsginfo.setString(3, address);
						pstmtinsertappmsginfo.setInt(4, port);
						pstmtinsertappmsginfo.execute();
						pstmtinsertappmsginfo.close();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				
				String sqlupdate = "update thing_device_info set aftersetcount=0  where id=?";
				PreparedStatement pstmt = con.prepareStatement(sqlupdate);;
				pstmt.setString(1, id);
				pstmt.executeUpdate();
				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				ConnectionManager.closeConnection(con, null);
			}
		}else{
			appagtersettercont = modelappmsg.getInt("aftersetcount");
			Connection con = null;
			try {
				con = ConnectionManager.getConnection(true);
				String sqlupdateappmsginfo = "update thing_appmsginfo set aftersetcount=aftersetcount+1 where id=? and address=? and port=?";
				PreparedStatement pstmtupdateappmsginfo = con.prepareStatement(sqlupdateappmsginfo);
				pstmtupdateappmsginfo.setString(1, id);
				pstmtupdateappmsginfo.setString(2, address);
				pstmtupdateappmsginfo.setInt(3, port);
				pstmtupdateappmsginfo.executeUpdate();
				pstmtupdateappmsginfo.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				ConnectionManager.closeConnection(con, null);
			}
		}
		
		if(!isupdatesetter && modelappmsg.getInt("serviceaftersetcount") < 3){
			isupdatesetter = true;
		}
		

		Record model = Db.findFirst(
				"select *,(select onoroff from thing_device where id = thing_device_recievedata.id limit 1) onoroff from thing_device_recievedata where id = ? order by recievedate desc  limit 1", id);
		// Recievedata model =
		// me.findFirst("select *,(select * from thing_device where id = thing_device_recievedata.id) onoroff from thing_device_recievedata where id = ? order by recievedate desc ",id);
		DeviceConfigure localconfig =  DeviceConfigure.me.findById(id);
		
		boolean off = false;
		
		if(localconfig != null){
			int setonoroff = localconfig.getInt("setonoroff");
			if(setonoroff == 1){
				off = true;
			}
		}
		
		StringBuffer sb = new StringBuffer("40 DA ");
		if (model != null) {
			String s = null;
			int count = 0;
				
			s = "00".equals(model.getStr("frequency")) ? model.getStr("frequency") : (isupdatesetter ? (appagtersettercont > 2 ? Integer.toHexString(localconfig.getInt("frequency")).toUpperCase() : ss.split(" ")[3]) : model.getStr("frequency"));
			sb.append((s.length() < 2 ? "0" + s : s) + " ");
			count += Integer.parseInt(s,16);

			s = model.getStr("formaldehyde");
			sb.append(s + " ");
			count += Integer.parseInt(s,16);
			
			String modeindevice = new Integer(Integer.parseInt(model.getStr("mode"),16)).toString();
			String modeinappmsg = appagtersettercont > 2 ? localconfig.getInt("model").toString() : new Integer(Integer.parseInt(ss.split(" ")[4],16)).toString();
			String setmode = (modeindevice.length() > 1?modeindevice.substring(0,1):"0") + (modeinappmsg.length() > 1?modeinappmsg.substring(1):modeinappmsg);
			String hexsetmode = Integer.toHexString(new Integer(setmode)).toUpperCase();
			s = isupdatesetter ? (hexsetmode.length() < 2 ? "0" + hexsetmode : hexsetmode) : model.getStr("mode");
//			s = isupdatesetter ? (model.getStr("mode").substring(0,1) + ss.split(" ")[4].substring(1)) : model.getStr("mode");
			sb.append(s + " ");
			count += Integer.parseInt(s,16);
			
			String modeswitchindevice = new Integer(Integer.parseInt(model.getStr("modeswitch"),16)).toString();
			String modeswitchinappmsg = appagtersettercont > 2 ? localconfig.getInt("functionalswitch").toString() :  new Integer(Integer.parseInt(ss.split(" ")[5],16)).toString();
			String setmodeswitch = (modeswitchindevice.length() > 1 ? modeswitchindevice.substring(0,1):"0") + (modeswitchinappmsg.length() > 1?modeswitchinappmsg.substring(1):modeswitchinappmsg);
			String hexsetmodeswitch = Integer.toHexString(new Integer(setmodeswitch)).toUpperCase();
			s = isupdatesetter ? (hexsetmodeswitch.length() < 2 ? "0" + hexsetmodeswitch : hexsetmodeswitch) : model.getStr("modeswitch");
//			s = isupdatesetter ? (model.getStr("modeswitch").substring(0,1) + ss.split(" ")[5].substring(1)) : model.getStr("modeswitch");
			sb.append(s + " ");
			count += Integer.parseInt(s,16);

			s = model.getStr("pm25inhigh");
			sb.append(s + " ");
			count += Integer.parseInt(s,16);

			s = model.getStr("pm25inlow");
			sb.append(s + " ");
			count += Integer.parseInt(s,16);

			s = model.getStr("pm25outhigh");
			sb.append(s + " ");
			count += Integer.parseInt(s,16);

			s = model.getStr("pm25outlow");
			sb.append(s + " ");
			count += Integer.parseInt(s,16);
			
			if(off){
				s = model.getStr("co2high");
				sb.append("01 ");
				count += Integer.parseInt("01",16);

				s = model.getStr("co2low");
				sb.append("00 ");
				count += Integer.parseInt("00",16);
			}else{
				if(model.getInt("onoroff") == 0){
					s = model.getStr("co2high");
					sb.append("00 ");
					count += Integer.parseInt("00",16);
	
					s = model.getStr("co2low");
					sb.append("00 ");
					count += Integer.parseInt("00",16);
				}else{
					s = model.getStr("co2high");
					sb.append(s + " ");
					count += Integer.parseInt(s,16);
	
					s = model.getStr("co2low");
					sb.append(s + " ");
					count += Integer.parseInt(s,16);
				}
			}

		

			s = model.getStr("enteringairtempin");
			sb.append(s + " ");
			count += Integer.parseInt(s,16);

			s = model.getStr("leavingairtempin");
			sb.append(s + " ");
			count += Integer.parseInt(s,16);

			s = model.getStr("enteringairtempout");
			sb.append(s + " ");
			count += Integer.parseInt(s,16);

			s = model.getStr("leavingairtempout");
			sb.append(s + " ");
			count += Integer.parseInt(s,16);

			s = model.getStr("humidity");
			sb.append(s + " ");
			count += Integer.parseInt(s,16);

			// s = model.getStr("message");
			// sb.append(s + " ");
			//
			// s = model.getStr("directiveno");
			// sb.append(s + " ");

			String id1 = id.substring(0, 2);
			String id2 = id.substring(2, 4);
			String id3 = id.substring(4, 6);

			sb.append(id3 + " ");
			sb.append(id2 + " ");
			sb.append(id1 + " ");
			
			count += Integer.parseInt(id3,16);
			count += Integer.parseInt(id2,16);
			count += Integer.parseInt(id1,16);

			if(model.getInt("onoroff") == 0){
				s = Integer.toString(count, 16).toUpperCase();
				if (s.length() < 2) {
					sb.append("0" + s + " ");
				} else {
					if (s.length() > 2) {
						sb.append(s.substring(s.length() - 2) + " ");
					} else {
						sb.append(s + " ");
					}
				}
			}else{
				s = Integer.toString(count, 16).toUpperCase();
				if (s.length() < 2) {
					sb.append("0" + s + " ");
				} else {
					if (s.length() > 2) {
						sb.append(s.substring(s.length() - 2) + " ");
					} else {
						sb.append(s + " ");
					}
				}
				//sb.append(s + " ");
			}

			sb.append("AB");
		} else {
			String s = null;
			int count = 0;

			s = "00";
			sb.append(s + " ");

			s = "00";
			sb.append(s + " ");

			s = "00";
			sb.append(s + " ");

			s = "00";
			sb.append(s + " ");

			s = "00";
			sb.append(s + " ");

			s = "00";
			sb.append(s + " ");

			s = "00";
			sb.append(s + " ");

			s = "00";
			sb.append(s + " ");
			if(off){
				sb.append("01 ");
				count += Integer.parseInt("01",16);

				sb.append("00 ");
				count += Integer.parseInt("00",16);
			}else{
				s = "00";
				sb.append(s + " ");
	
				s = "00";
				sb.append(s + " ");
			}

			s = "00";
			sb.append(s + " ");

			s = "00";
			sb.append(s + " ");

			s = "00";
			sb.append(s + " ");

			s = "00";
			sb.append(s + " ");

			s = "00";
			sb.append(s + " ");

			String id1 = id.substring(0, 2);
			String id2 = id.substring(2, 4);
			String id3 = id.substring(4, 6);

			sb.append(id3 + " ");
			sb.append(id2 + " ");
			sb.append(id1 + " ");

			Integer id3num = Integer.valueOf(id3, 16);
			Integer id2num = Integer.valueOf(id2, 16);
			Integer id1num = Integer.valueOf(id1, 16);

			count += id3num;
			count += id2num;
			count += id1num;

			s = Integer.toString(count, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				if (s.length() > 2) {
					sb.append(s.substring(s.length() - 2) + " ");
				} else {
					sb.append(s + " ");
				}
			}

			sb.append("AB");
		}

		return sb.toString();

	}
	public String getDeviceDataInfo(String id,String ss) {
		
		
		//Record modelappmsg = Db.findFirst("select *,(select aftersetcount from thing_device_info where id = thing_appmsginfo.id limit 1) serviceaftersetcount from thing_appmsginfo where id=? and appmsg=?",id,ss) ;
		boolean isupdatesetter = false;
		int appagtersettercont = 3;
		
		
		Record model = Db.findFirst(
				"select *,(select onoroff from thing_device where id = thing_device_recievedata.id limit 1) onoroff from thing_device_recievedata where id = ? order by recievedate desc  limit 1", id);
		// Recievedata model =
		// me.findFirst("select *,(select * from thing_device where id = thing_device_recievedata.id) onoroff from thing_device_recievedata where id = ? order by recievedate desc ",id);
		DeviceConfigure localconfig = appagtersettercont > 2 ? DeviceConfigure.me.findById(id) : null;
		
		StringBuffer sb = new StringBuffer("40 DA ");
		if (model != null) {
			String s = null;
			int count = 0;
			
			s = isupdatesetter ? (appagtersettercont > 2 ? Integer.toHexString(localconfig.getInt("frequency")).toUpperCase() : ss.split(" ")[3]) : model.getStr("frequency");
			sb.append((s.length() < 2 ? "0" + s : s) + " ");
			count += Integer.parseInt(s,16);
			
			s = model.getStr("formaldehyde");
			sb.append(s + " ");
			count += Integer.parseInt(s,16);
			
			String modeindevice = new Integer(Integer.parseInt(model.getStr("mode"),16)).toString();
			String modeinappmsg = appagtersettercont > 2 ? localconfig.getInt("model").toString() : new Integer(Integer.parseInt(ss.split(" ")[4],16)).toString();
			String setmode = (modeindevice.length() > 1?modeindevice.substring(0,1):"0") + (modeinappmsg.length() > 1?modeinappmsg.substring(1):modeinappmsg);
			String hexsetmode = Integer.toHexString(new Integer(setmode)).toUpperCase();
			s = isupdatesetter ? (hexsetmode.length() < 2 ? "0" + hexsetmode : hexsetmode) : model.getStr("mode");
//			s = isupdatesetter ? (model.getStr("mode").substring(0,1) + ss.split(" ")[4].substring(1)) : model.getStr("mode");
			sb.append(s + " ");
			count += Integer.parseInt(s,16);
			
			String modeswitchindevice = new Integer(Integer.parseInt(model.getStr("modeswitch"),16)).toString();
			String modeswitchinappmsg = appagtersettercont > 2 ? localconfig.getInt("functionalswitch").toString() :  new Integer(Integer.parseInt(ss.split(" ")[5],16)).toString();
			String setmodeswitch = (modeswitchindevice.length() > 1 ? modeswitchindevice.substring(0,1):"0") + (modeswitchinappmsg.length() > 1?modeswitchinappmsg.substring(1):modeswitchinappmsg);
			String hexsetmodeswitch = Integer.toHexString(new Integer(setmodeswitch)).toUpperCase();
			s = isupdatesetter ? (hexsetmodeswitch.length() < 2 ? "0" + hexsetmodeswitch : hexsetmodeswitch) : model.getStr("modeswitch");
//			s = isupdatesetter ? (model.getStr("modeswitch").substring(0,1) + ss.split(" ")[5].substring(1)) : model.getStr("modeswitch");
			sb.append(s + " ");
			count += Integer.parseInt(s,16);
			
			s = model.getStr("pm25inhigh");
			sb.append(s + " ");
			count += Integer.parseInt(s,16);
			
			s = model.getStr("pm25inlow");
			sb.append(s + " ");
			count += Integer.parseInt(s,16);
			
			s = model.getStr("pm25outhigh");
			sb.append(s + " ");
			count += Integer.parseInt(s,16);
			
			s = model.getStr("pm25outlow");
			sb.append(s + " ");
			count += Integer.parseInt(s,16);
			
			if(model.getInt("onoroff") == 0){
				s = model.getStr("co2high");
				sb.append("00 ");
				count += Integer.parseInt("00",16);
				
				s = model.getStr("co2low");
				sb.append("00 ");
				count += Integer.parseInt("00",16);
			}else{
				s = model.getStr("co2high");
				sb.append(s + " ");
				count += Integer.parseInt(s,16);
				
				s = model.getStr("co2low");
				sb.append(s + " ");
				count += Integer.parseInt(s,16);
			}
			
			
			
			s = model.getStr("enteringairtempin");
			sb.append(s + " ");
			count += Integer.parseInt(s,16);
			
			s = model.getStr("leavingairtempin");
			sb.append(s + " ");
			count += Integer.parseInt(s,16);
			
			s = model.getStr("enteringairtempout");
			sb.append(s + " ");
			count += Integer.parseInt(s,16);
			
			s = model.getStr("leavingairtempout");
			sb.append(s + " ");
			count += Integer.parseInt(s,16);
			
			s = model.getStr("humidity");
			sb.append(s + " ");
			count += Integer.parseInt(s,16);
			
			// s = model.getStr("message");
			// sb.append(s + " ");
			//
			// s = model.getStr("directiveno");
			// sb.append(s + " ");
			
			String id1 = id.substring(0, 2);
			String id2 = id.substring(2, 4);
			String id3 = id.substring(4, 6);
			
			sb.append(id3 + " ");
			sb.append(id2 + " ");
			sb.append(id1 + " ");
			
			count += Integer.parseInt(id3,16);
			count += Integer.parseInt(id2,16);
			count += Integer.parseInt(id1,16);
			
			if(model.getInt("onoroff") == 0){
				s = Integer.toString(count, 16).toUpperCase();
				if (s.length() < 2) {
					sb.append("0" + s + " ");
				} else {
					if (s.length() > 2) {
						sb.append(s.substring(s.length() - 2) + " ");
					} else {
						sb.append(s + " ");
					}
				}
			}else{
				s = model.getStr("checksum");
				sb.append(s + " ");
			}
			
			sb.append("AB");
		} else {
			String s = null;
			int count = 0;
			
			s = "00";
			sb.append(s + " ");
			
			s = "00";
			sb.append(s + " ");
			
			s = "00";
			sb.append(s + " ");
			
			s = "00";
			sb.append(s + " ");
			
			s = "00";
			sb.append(s + " ");
			
			s = "00";
			sb.append(s + " ");
			
			s = "00";
			sb.append(s + " ");
			
			s = "00";
			sb.append(s + " ");
			
			s = "00";
			sb.append(s + " ");
			
			s = "00";
			sb.append(s + " ");
			
			s = "00";
			sb.append(s + " ");
			
			s = "00";
			sb.append(s + " ");
			
			s = "00";
			sb.append(s + " ");
			
			s = "00";
			sb.append(s + " ");
			
			s = "00";
			sb.append(s + " ");
			
			String id1 = id.substring(0, 2);
			String id2 = id.substring(2, 4);
			String id3 = id.substring(4, 6);
			
			sb.append(id3 + " ");
			sb.append(id2 + " ");
			sb.append(id1 + " ");
			
			Integer id3num = Integer.valueOf(id3, 16);
			Integer id2num = Integer.valueOf(id2, 16);
			Integer id1num = Integer.valueOf(id1, 16);
			
			count += id3num;
			count += id2num;
			count += id1num;
			
			s = Integer.toString(count, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				if (s.length() > 2) {
					sb.append(s.substring(s.length() - 2) + " ");
				} else {
					sb.append(s + " ");
				}
			}
			
			sb.append("AB");
		}
		
		return sb.toString();
		
	}

	public String getAppSetterSaveSuccessInfo(String id) {

		StringBuffer sb = new StringBuffer("40 EA ");
			String s = null;
			int count = 0;

			s = "00";
			sb.append(s + " ");

			s = "00";
			sb.append(s + " ");

			s = "00";
			sb.append(s + " ");

			s = "00";
			sb.append(s + " ");

			s = "00";
			sb.append(s + " ");

			s = "00";
			sb.append(s + " ");

			s = "00";
			sb.append(s + " ");

			s = "00";
			sb.append(s + " ");

			s = "00";
			sb.append(s + " ");

			s = "00";
			sb.append(s + " ");

			s = "00";
			sb.append(s + " ");

			s = "00";
			sb.append(s + " ");

			s = "00";
			sb.append(s + " ");

			s = "00";
			sb.append(s + " ");

			s = "00";
			sb.append(s + " ");

			String id1 = id.substring(0, 2);
			String id2 = id.substring(2, 4);
			String id3 = id.substring(4, 6);

			sb.append(id3 + " ");
			sb.append(id2 + " ");
			sb.append(id1 + " ");

			Integer id3num = Integer.valueOf(id3, 16);
			Integer id2num = Integer.valueOf(id2, 16);
			Integer id1num = Integer.valueOf(id1, 16);

			count += id3num;
			count += id2num;
			count += id1num;

			s = Integer.toString(count, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				if (s.length() > 2) {
					sb.append(s.substring(s.length() - 2) + " ");
				} else {
					sb.append(s + " ");
				}
			}

			sb.append("AB");

		return sb.toString();

	}

}
